package priv.dk.springboot.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import priv.dk.springboot.exception.BusinessException;

import static priv.dk.springboot.util.CommonUtil.NO_LOGIN;

import java.io.IOException;

// @WebFilter(filterName = "LoginFiler",urlPatterns = "/*")
public class LoginFiler implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpSession session = ((HttpServletRequest) servletRequest).getSession();
        Object employee = session.getAttribute("employee");
        // 未登录处理
        if (employee == null) throw new BusinessException(NO_LOGIN, "未登录");
        // 放行
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
